/**
 * Created by 功振 on 2016/2/2.
 */
angular.module('NatureCloud').controller('BuildStartController', ["$scope", "$uibModalInstance", "$cookies", "buildStart", "projectCodeInfoService", "params",
    function ($scope, $uibModalInstance, $cookies, buildStart, projectCodeInfoService, params) {
        var user = $cookies.get("username");
        $scope.tags_loading = true;
        //default value is 'master'
        $scope.sub = {};
        $scope.sub.version = "master";
        $scope.type = params.type;
        $scope.sub.branchtype = 'branch';
        $scope.dockerfile = "";

        /*
        *   not support get branch in node.js side, so add default 'master ' branch in frontend
        * */

        if(params.idTagMap.hasOwnProperty(params.id)){
            $scope.versionOptions = params.idTagMap[params.id];
            if($scope.versionOptions.indexOf('master') === -1)
                $scope.versionOptions.push('master');
            $scope.sub.version = $scope.versionOptions[$scope.versionOptions.length - 1];
            $scope.tags_loading = false;
        }else{

            if(params.type === 'git' || params.type === 'svn'){
                $scope.isManual = true;

            }else{
                $scope.isManual = false;
                projectCodeInfoService(user, params.type, params.full_name).then(function (response) {
                    if(response.data instanceof Array){
                        params.idTagMap[params.id] = response.data;
                        $scope.versionOptions = response.data;
                        if($scope.versionOptions.indexOf('master') === -1)
                            $scope.versionOptions.push('master');
                        $scope.sub.version = $scope.versionOptions[$scope.versionOptions.length - 1];
                        $scope.tags_loading = false;
                        console.log("Code Tag list get succeeded");
                    }else if(response.status === 200 && response.data === 'Forbidden'){
                        alertify.alert("build operate error","Forbidden to get Code Tag list");
                        //$uibModalInstance.dismiss('cancel');
                    }
                    else{
                        window.location.href = response.url;
                    }

                }, function (response) {
                    // alert fail
                    alertify.alert("build operate error","Code Tag list get failed");
                });
            }
        }


    $scope.ok = function () {
        var user = $cookies.get("username");
        if($scope.sub.version !== undefined && $scope.sub.version !== ''){
            buildStart(user, params, $scope.sub.version,$scope.sub.branchtype,$scope.dockerfile).then(function (r) {

                if (r.ret === true){
                    alertify.success(params.id + "build start " + params.id + " " + $scope.sub.version + " succeeded");
                }else{
                    alertify.error(params.id + " build start failed");
                }


            }, function (r) {

                    alertify.error(params.id + " build start failed");
            });
            $uibModalInstance.close();
             window.history.replaceState({},0,'http://'+window.location.host+'/cp/buildlist/');

        }
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
         window.history.replaceState({},0,'http://'+window.location.host+'/cp/buildlist/');
    };
    }]);